package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class EcohoClient extends JFrame {

	BufferedWriter bw = null;
	
	private JPanel contentPane;
	private JPanel panel;
	private JLabel lblNewLabel;
	private JTextField ips;
	private JLabel lblNewLabel_1;
	private JTextField port;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextPane textPane;
	private JTextField str1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EcohoClient frame = new EcohoClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EcohoClient() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 679, 299);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
	}

	public void send1(){
		btnNewButton_1.setEnabled(false);
		btnNewButton.setEnabled(true);

		String ip = ips.getText();
		int p = Integer.parseInt(port.getText());
		try {
			Socket s = new Socket(ip, p);
			OutputStream os = s.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
			bw = new BufferedWriter(osw);
			
			
			String str2 = str1.getText();
//			send(str2+"\n");
			str1.setText("");
			//send("특명:변기를 막아랏!!!!!!!!"+"\n");
			bw.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
//	public void send(String str) throws Exception{
//		try{
//		bw.write(str); // 전송~
//		bw.newLine();
//		bw.flush();
//		data.append(str+"<br/>");
//		result.setText(data.toString());
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}
//	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			panel.add(getLblNewLabel());
			panel.add(getIps());
			panel.add(getLblNewLabel_1());
			panel.add(getPort());
			panel.add(getBtnNewButton_1());
			panel.add(getBtnNewButton());
			panel.add(getStr1());
		}
		return panel;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Server 주소");
		}
		return lblNewLabel;
	}

	private JTextField getIps() {
		if (ips == null) {
			ips = new JTextField();
			ips.setColumns(10);
			try {
				InetAddress ia = InetAddress.getLocalHost();
				ips.setText(ia.getHostAddress());
				setTitle(ia.getHostAddress());
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return ips;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("포트");
		}
		return lblNewLabel_1;
	}

	private JTextField getPort() {
		if (port == null) {
			port = new JTextField();
			port.setHorizontalAlignment(SwingConstants.RIGHT);
			port.setText("4444");
			port.setColumns(10);
		}
		return port;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("접속해제");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btnNewButton_1.setEnabled(true);
					btnNewButton.setEnabled(false);
				}
			});
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("서버접속");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					send1();}
				
//					btnNewButton_1.setEnabled(false);
//					btnNewButton.setEnabled(true);
//
//					String ip = ips.getText();
//					int p = Integer.parseInt(port.getText());
//					try {
//						Socket s = new Socket(ip, p);
//						OutputStream os = s.getOutputStream();
//						OutputStreamWriter osw = new OutputStreamWriter(os);
//						bw = new BufferedWriter(osw);
//						
//						String str2 = str1.getText();
//						send(str2+"\n");
//						//send("특명:변기를 막아랏!!!!!!!!"+"\n");
//						bw.close();
//					} catch (Exception ex) {
//						ex.printStackTrace();
//					}
//				}
			});
	}
		return btnNewButton_1;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextPane());
		}
		return scrollPane;
	}

	private JTextPane getTextPane() {
		if (textPane == null) {
			textPane = new JTextPane();
		}
		return textPane;
	}
	private JTextField getStr1() {
		if (str1 == null) {
			str1 = new JTextField();
			str1.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					
					if (e.getKeyCode() == KeyEvent.VK_ENTER)
						send1();
				
				}
			});
			str1.setColumns(10);
		}
		return str1;
	}
}
